package com.young.sync.test;

import com.young.canal.serializer.EventHeader;
import com.young.canal.serializer.EventRecord;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.common.serialization.StringSerializer;
import org.springframework.kafka.support.serializer.JsonSerializer;

import java.util.Properties;

public class ProducerDemo {
    public static void main(String[] args) {
        //0.创建kafka生产者的配置对象
        Properties properties = new Properties();
        //生产者连接集群所需的 broker 地 址 清 单
        properties.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG,"hadoop102:9092");
        //指定发送消息的 key 和 value 的序列化类型
        properties.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
        properties.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, JsonSerializer.class.getName());




        //1.创建kafka生产者对象
        KafkaProducer<String, EventRecord> kafkaProducer = new KafkaProducer<String, EventRecord>(properties);

        EventRecord record = new EventRecord();
        EventHeader header = new EventHeader();
        header.setSchemaName("order");
        header.setTableName("user");
        header.setEventType(1);
        record.setHeader(header);

        record.setSql("select * from user");

        kafkaProducer.send(new ProducerRecord<String, EventRecord>("order",record));

        //3.关闭资源
        kafkaProducer.close();
    }
}
